package com.weic.easycache.utils;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.weic.easycache.exception.XmlResourseException;

import android.content.res.XmlResourceParser;
import android.util.AndroidRuntimeException;
import android.util.SparseArray;

public class LayoutIdToClassesParser {
	private SparseArray<String> mLayoutIdToMediatorMap, mLayoutIdToProxyMap;
	/**匹配类名或者包路径**/
	private final String mRegex = "([A-Za-z_$]+\\d*[A-Za-z_$]*\\.)*[A-Za-z_$]+\\d*[A-Za-z_$]*";

	public SparseArray<String> getLayoutIdToMediatorMap() {
		if(mLayoutIdToMediatorMap == null) {
			throw new AndroidRuntimeException("还没有解析资源！~");
		}
		return mLayoutIdToMediatorMap;
	}

	public SparseArray<String> getLayoutIdToProxyMap() {
		if(mLayoutIdToProxyMap == null) {
			throw new AndroidRuntimeException("还没有解析资源！~");
		}
		return mLayoutIdToProxyMap;
	}

	public void buildMap(XmlResourceParser mParser) throws XmlPullParserException,
	IOException, XmlResourseException {
		if (mParser == null) {
			throw new IllegalArgumentException("要解析的资源为空！~");
		}
		mLayoutIdToMediatorMap = new SparseArray<String>();
		mLayoutIdToProxyMap = new SparseArray<String>();

		String mediator = null, proxy = null;
		int layoutId = 0;

		/**
		 * 目前已知编译器会将layout和drawable下的某些资源属性“@../..”编译成“@1234567”(数字表示ResourceId)，
		 * 但是“@style/..”不会转换。不过不管是否会转换，都可以通过attrs或mParser.getAttributeResourceValue(i, -123)获取到。
		 */
		/*	源码使用空循环给AttributeSet赋了值
		 * 这种方式，只从开头读取到第一个开始标记为止，这样读到的就是根标签的属性

      	while ((eventType = mParser.next()) != XmlPullParser.START_TAG &&
    		eventType != XmlPullParser.END_DOCUMENT) {
    		//Empty
    	}

		if (eventType != XmlPullParser.START_TAG) {
    		throw new InflateException(mParser.getPositionDescription()
     	       + ": No start tag found!");
		}//*/

		int eventType = 0;
		try {
			while ((eventType = mParser.next()) != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.CDSECT:
					break;
				case XmlPullParser.COMMENT:
					break;
				case XmlPullParser.DOCDECL:
					break;
				case XmlPullParser.ENTITY_REF:
					break;
				case XmlPullParser.IGNORABLE_WHITESPACE:
					break;
				case XmlPullParser.PROCESSING_INSTRUCTION:
					break;
				case XmlPullParser.START_TAG:
					/*
						System.out.println("START_TAG:"+mParser.getName());
						System.out.println("AttributeCount:"+mParser.getAttributeCount());
						for(int i=0; i<attrs.getAttributeCount(); i++) {
							System.out.println(attrs.getAttributeName(i)+" = "+attrs.getAttributeValue(i));
							System.out.println(attrs.getAttributeName(i)+" = "+attrs.getAttributeResourceValue(i, -123));
						}//*/
					if(mParser.getName().equals("map")) {
						if(mParser.getAttributeCount()>0) {
							for(int i=0; i<mParser.getAttributeCount(); i++) {
								if(mParser.getAttributeName(i).equals("layoutid")) {
									layoutId = mParser.getAttributeResourceValue(i, 0);
									//mediator = mParser.nextText();
									//System.out.println(layoutId+"-->"+className);
								}else if(mParser.getAttributeName(i).equals("mediator")) {
									mediator = mParser.getAttributeValue(i).trim();
								}else if(mParser.getAttributeName(i).equals("proxy")) {
									proxy = mParser.getAttributeValue(i).trim();
								}
							}
							if(layoutId > 0x7f000000) {
								if(mediator.matches(mRegex)) {
									mLayoutIdToMediatorMap.put(layoutId, mediator);
								}else {
									throw new XmlResourseException("配置文件中mediator所指定的类名" +
											"全路径名不正确，请检查！~");
								}
								if(proxy.matches(mRegex)) {
									mLayoutIdToProxyMap.put(layoutId, proxy);
								}else {
									throw new XmlResourseException("配置文件中proxy所指定的类名" +
											"全路径名不正确，请检查！~");
								}
							}else {
								throw new XmlResourseException("配置文件中layoutid值不正确，请检查！~");
							}
						}
					}
					break;
				case XmlPullParser.END_TAG:
					break;
				case XmlPullParser.TEXT:
					break;
				default:
					break;
				}
			}
		}finally {
			mParser.close();	//这个必须放在最后
		}//*/
	}
}
